#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>

using namespace std;

class Solution
{
public:
    bool wordBreak(string s, vector<string>& wordDict)
    {
        unordered_set<string> hash;
        for (auto& s : wordDict) hash.insert(s);

        int n = s.size();
        vector<bool> dp(n + 1);
        dp[0] = true;
        s = " " + s;
        for (int i = 1; i <= n; i++)
        {
            for (int j = i; j >= 1; j--)
            {
                if (dp[j - 1] && hash.count(s.substr(j, i - j + 1)))
                {
                    dp[i] = true;
                    if (dp[i]) break;
                }
            }
        }

        return dp[n];
    }
};

int main()
{
    string s = "leetcode";
    vector<string> words = { "leet", "code" };
    cout << Solution().wordBreak(s, words);// true
    return 0;
}
